package xyz.xtt.exchange.service.impl;

import com.google.common.base.Splitter;
import com.google.common.collect.Maps;
import com.google.common.primitives.Longs;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import xyz.xtt.exchange.service.IRedisCacheService;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

@Service
public class RedisCacheServiceImpl implements IRedisCacheService {
    @Value("${asset.rose.fakeStart}")
    private String fakeStart;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public Map<Long, Long> getSeqNo() {
        BoundHashOperations<String, String, String> boundHashOps = stringRedisTemplate.boundHashOps("nft:seqno:fake");
        Set<String> keys = boundHashOps.keys();
        HashMap<Long, Long> newHashMap = Maps.newHashMap();
        if (keys != null && !keys.isEmpty()) {
            keys.forEach(k -> {
                newHashMap.put(Longs.tryParse(k), Longs.tryParse(Objects.requireNonNull(boundHashOps.get(k))));
            });
            return newHashMap;
        }
        // 从配置中读取默认值
        Map<String, String> fakeLimit = Splitter.on("&").withKeyValueSeparator("=").split(fakeStart);
        fakeLimit.forEach((key, value) -> {
            newHashMap.put(Longs.tryParse(key), Longs.tryParse(value));
        });
        return newHashMap;
    }
}
